package playlist;

import java.util.Hashtable;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import function.SearchKeyHandler;
import graphics.SearchInput;

public class MainScreen extends Canvas implements CommandListener {

	private Crawler midlet;
	private Image image;
	private Graphics graphics;
	private String title = null;
	private final int LABEL_WIDTH = 22;
	private String alert = null;
	private int Y_TEXT_OFFSET = 17;

	private final Command searchCommand = new Command("Search", Command.ITEM, 1);
	private final Command pauseCommand = new Command("Pause", Command.EXIT, 1);
	private final Command playCommand = new Command("Play", Command.EXIT, 1);
	private final Command cancelSearchCommand = new Command("Cancel", Command.EXIT, 1);
	private boolean searchEngaged = false;
	private SearchKeyHandler searchKeyHandler = null;
	private Hashtable displayedCommand = new Hashtable();
	private SearchInput searchInput = null;

	public MainScreen(Crawler aMidlet) throws Throwable {
		midlet = aMidlet;
		image = Image.createImage("/Fond.gif");
		setCommandListener(this);
	}

	protected void paint(Graphics g) {
		graphics = g;
		if (image != null) {
			g.drawImage(image, 0, 0, Graphics.LEFT | Graphics.TOP);
		}
		graphics.setColor(0x000000);
		graphics.drawRect(0, 0, 240 - 6, 27);
		
		if (searchInput != null) {
			searchInput.repaint(g);
		} else if (alert != null) {
			drawAlert();
		} else if (title != null) {
			drawTitle();
		}
	}

	private void drawAlert() {
		graphics.setColor(0x000000);
		graphics.drawRect(0, Y_TEXT_OFFSET, 240, 10);
		graphics.setColor(0xFF9900);

		int offset = (int) (LABEL_WIDTH - 2 - alert.length()) * 240
				/ LABEL_WIDTH / 2;

		graphics.drawString(alert, offset, Y_TEXT_OFFSET, Graphics.LEFT
				| Graphics.TOP);
	}

	public void keyPressed(int keyCode) {
		if (searchEngaged) {
			searchKeyHandler.newStroke(keyCode);
		} else {
			midlet.processKey(keyCode);
		}
	}

	public void drawTitle() {
		graphics.setColor(0x000000);
		graphics.drawRect(0, Y_TEXT_OFFSET, 240, 10);
		graphics.setColor(0xffffff);

		int offset = (int) (LABEL_WIDTH - title.length()) * 240 / LABEL_WIDTH
				/ 2;

		graphics.drawString(title, offset, Y_TEXT_OFFSET, Graphics.LEFT
				| Graphics.TOP);
	}

	public void setTitle(String aTitle) {
		title = aTitle;
	}

	public void setAlert(String message) {
		alert = message;
	}

	public void setPauseAvailable(boolean available) {
//		if (available) {
//			secureRemoveCommand(playCommand);
//			secureAddCommand(pauseCommand);
//		} else {
//			secureRemoveCommand(pauseCommand);
//		}
	}

	public void setPlayAvailable(boolean available) {
//		if (available) {
//			secureRemoveCommand(pauseCommand);
//			secureAddCommand(playCommand);
//		} else {
//			secureRemoveCommand(playCommand);
//		}
	}

	public void setSearchAvailable(boolean available) {
		if (available) {
			secureAddCommand(searchCommand);
			secureRemoveCommand(cancelSearchCommand);
		} else {
			secureRemoveCommand(searchCommand);
			secureAddCommand(cancelSearchCommand);
		}
	}

	public void commandAction(Command command, Displayable dis) {

		if (command == cancelSearchCommand) {
			cancelSearch();
		}
		
		if (searchEngaged)
			return;

		if (command == playCommand) {
			midlet.playPlayer();
			setPauseAvailable(true);
		} else if (command == pauseCommand) {
			midlet.pausePlayer();
			setPlayAvailable(true);
		} else if (command == searchCommand) {
			setSearchAvailable(false);
			showSearchInput(true);
			if ((searchKeyHandler) == null) {
				searchKeyHandler = new SearchKeyHandler();
			}
			searchEngaged = true;
		}
	}

	public void searchCompleted() {
		searchEngaged = false;
		searchKeyHandler = null;
	}

	public void cancelSearch() {
		showSearchInput(false);
		searchEngaged = false;
		midlet.alert("Search canceled.");
		setSearchAvailable(true);
	}

	private void secureAddCommand(Command command) {
		if (displayedCommand.get(command) == null) {
			displayedCommand.put(command, "");
			addCommand(command);
		}
	}

	private void secureRemoveCommand(Command command) {
		if (displayedCommand.get(command) != null) {
			displayedCommand.remove(command);
			removeCommand(command);
		}
	}

	public void searchDisplayUpdate(char value) {
		searchInput.charInput(value);
	}

	public void shiftSearchCursor() {
		searchInput.shiftCursor(1);
	}

	public void showSearchInput(boolean value) {
		if (value) {
			searchInput = new SearchInput(this, 2);
			searchInput.setColors(0x000000, 0xFF9900);
			searchInput.setCursorAt(0);
			searchInput.setY(Y_TEXT_OFFSET);
		} else {
			searchInput = null;
		}
	}
}
